package com.candoit.webapps.pricing.controllers;

import javax.persistence.EntityManager;

import org.springframework.transaction.annotation.Transactional;

import com.candoit.webapps.pricing.bean.PricingContext;

public class AbstractController {
	
	protected boolean saveSuccess;
	
	protected boolean deleteSuccess;
	
	protected String message;
	
	protected boolean error;
	
	protected static String NAME_CC_EMPTY = "Nombre de centro de costo vacio";
	
	protected static String DESCRIPTION_PROFILE_EMPTY = "Descripción del perfil vacia";
	
	protected static String USER_NAME_EMPTY = "Nombre de usuario vacio"; 
	
	protected static String PASSWORD_DISTINTIC = "Constraseña no válida";
	
	protected static String NAME_USER_EMPTY = "Nombre de usuario vacio";
	
	protected static String SURNAME_USER_EMPTY = "Apellido de usuario vacio";
	
	protected static String USER_NAME_DUPLICATE = "Nombre de usuario existente";
	
	protected static String NAME_MP_EMPTY = "Nombre de materia prima vacio";
	
	protected static String NAME_LINE_EMPTY = "Nombre de linea vacio";
	
	protected static String NAME_INITIAL_EMPTY = "inicial vacia";
	
	protected static String NAME_MACHINE_EMPTY = "Nombre de máquina vacia";
	
	protected static String NAME_MACHINE_EXISTS = "Nombre de máquina ya existe";
	
	protected static String CODE_MODEL_EMPTY = "Código de módelo vacio";
	
	protected static String DESCRIPCION_ARTICULO_EMPTY = "Descripción de artículo vacio";

	protected static String CODIGO_ARTICULO_EXISTS = "Codigo de articulo ya existe";
	
	protected static String ALTO_EMPTY = "El alto de la medida no debe ser vacio";
	
	protected static String ANCHO_EMPTY = "El ancho de la medida no debe ser vacio";
	
	protected static String LARGO_EMPTY = "El largo de la medida no debe ser vacio";
	
	public boolean isSaveSuccess() {
		return saveSuccess;
	}

	public void setSaveSuccess(boolean saveSuccess) {
		this.saveSuccess = saveSuccess;
	}
	
	public boolean isDeleteSuccess() {
		return deleteSuccess;
	}

	public void setDeleteSuccess(boolean deleteSuccess) {
		this.deleteSuccess = deleteSuccess;
	}
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public boolean isError() {
		return error;
	}

	public void setError(boolean error) {
		this.error = error;
	}
	
	@Transactional
	public void save(Object o){
		EntityManager em = PricingContext.getInstance().getEntityManager();
		em.persist(o);
		if (!em.getTransaction().isActive()){
			em.getTransaction().begin();
		}	
		em.getTransaction().commit();
		saveSuccess = true;
	}
	
	@Transactional
	public void remove(Object o){
		EntityManager em = PricingContext.getInstance().getEntityManager();
		em.remove(o);
		if (!em.getTransaction().isActive()){
			em.getTransaction().begin();
		}	
		em.getTransaction().commit();
		deleteSuccess = true;
	}
	
	@Transactional
	public void update(Object o){
		EntityManager em = PricingContext.getInstance().getEntityManager();
		em.merge(o);
		if (!em.getTransaction().isActive()){
			em.getTransaction().begin();
		}	
		em.getTransaction().commit();
		saveSuccess = true;
	}

}
